﻿Imports System
Imports System.Collections.Generic
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.ComponentModel.Compiler
    Friend NotInheritable Class TransactionContextValidator
        Inherits Validator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim errors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim activity As Activity = [TryCast](obj,Activity)
            If (activity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(Activity).FullName }), "obj")
            End If
            Dim transactionOptions As WorkflowTransactionOptions = TransactedContextFilter.GetTransactionOptions(activity)
            If (Not transactionOptions Is Nothing) Then
                If (Not FaultAndCancellationHandlingFilter.GetFaultHandlers(activity) Is Nothing) Then
                    Dim item As New ValidationError(SR.GetString("Error_AtomicScopeWithFaultHandlersActivityDecl", New Object() { activity.Name }), &H52C)
                    errors.Add(item)
                End If
                If (Not FaultAndCancellationHandlingFilter.GetCancellationHandler(activity) Is Nothing) Then
                    Dim error2 As New ValidationError(SR.GetString("Error_AtomicScopeWithCancellationHandlerActivity", New Object() { activity.Name }), &H575)
                    errors.Add(error2)
                End If
                Dim activity4 As Activity = activity.Parent
                Do While (Not activity4 Is Nothing)
                    If activity4.SupportsTransaction Then
                        errors.Add(New ValidationError(SR.GetString("Error_AtomicScopeNestedInNonLRT"), &H52E))
                        Exit Do
                    End If
                    activity4 = activity4.Parent
                Loop
                Dim queue As New Queue([Of] Activity)(Helpers.GetAllEnabledActivities(DirectCast(activity, CompositeActivity)))
                Do While (queue.Count > 0)
                    Dim activity5 As Activity = queue.Dequeue
                    If activity5.PersistOnClose Then
                        errors.Add(New ValidationError(SR.GetString("Error_LRTScopeNestedInNonLRT"), &H52F))
                        Exit Do
                    End If
                    If TypeOf activity5 Is ICompensatableActivity Then
                        errors.Add(New ValidationError(SR.GetString("Error_NestedCompensatableActivity", New Object() { activity5.QualifiedName }), &H1A6))
                        Exit Do
                    End If
                    If TypeOf activity5 Is CompositeActivity Then
                        Dim activity6 As Activity
                        For Each activity6 In Helpers.GetAllEnabledActivities(DirectCast(activity5, CompositeActivity))
                            queue.Enqueue(activity6)
                        Next
                    End If
                Loop
                If (transactionOptions.TimeoutDuration.Ticks < 0) Then
                    Dim error3 As New ValidationError(SR.GetString("Error_NegativeValue", New Object() { transactionOptions.TimeoutDuration.ToString, "TimeoutDuration" }), &H531) { _
                        .PropertyName = "TimeoutDuration" _
                    }
                    errors.Add(error3)
                End If
            End If
            Return errors
        End Function

        Public Overrides Function ValidateActivityChange(ByVal activity As Activity, ByVal action As ActivityChangeAction) As ValidationError
            If (activity Is Nothing) Then
                Throw New ArgumentNullException("activity")
            End If
            If (action Is Nothing) Then
                Throw New ArgumentNullException("action")
            End If
            Dim action2 As AddedActivityAction = [TryCast](action,AddedActivityAction)
            If (Not action2 Is Nothing) Then
                Dim queue As New Queue([Of] Activity)
                queue.Enqueue(action2.AddedActivity)
                Do While (queue.Count <> 0)
                    Dim activity2 As Activity = queue.Dequeue
                    If activity2.SupportsTransaction Then
                        Return New ValidationError(SR.GetString("Error_AtomicScopeNestedInNonLRT"), &H52E)
                    End If
                    If activity2.PersistOnClose Then
                        Return New ValidationError(SR.GetString("Error_NestedPersistOnClose", New Object() { activity.QualifiedName }), &H1A2)
                    End If
                    If TypeOf activity2 Is ICompensatableActivity Then
                        Return New ValidationError(SR.GetString("Error_NestedCompensatableActivity", New Object() { activity.QualifiedName }), &H1A6)
                    End If
                    Dim activity3 As CompositeActivity = [TryCast](activity2,CompositeActivity)
                    If (Not activity3 Is Nothing) Then
                        Dim activity4 As Activity
                        For Each activity4 In activity3.EnabledActivities
                            queue.Enqueue(activity4)
                        Next
                    End If
                Loop
            End If
            Return MyBase.ValidateActivityChange(activity, action)
        End Function

    End Class
End Namespace

